﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.UI.Common;

namespace Besture.UI.Portal.UserCenter
{
    public partial class UserInfoEdit : UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtPhone.Value = LoginUserInfo.Phone;
                txtEmail.Value = LoginUserInfo.Email;
                txtNickName.Value = LoginUserInfo.NickName;
                txtRealName.Value = LoginUserInfo.RealName;
                if (LoginUserInfo.Sex == 1) rb1.Enabled = true; else rb0.Enabled = true;
                txtBirthday.Value = !string.IsNullOrEmpty(LoginUserInfo.Birthday.ToString()) ? Convert.ToDateTime(LoginUserInfo.Birthday).ToString("yyyy-MM-dd") : DateTime.Now.ToString("yyyy-MM-dd");
                txtRealName.Value = LoginUserInfo.RealName;
                txtAreaCode.Value = LoginUserInfo.AreaCode;
                txtTel.Value = LoginUserInfo.Tel;
                txtTelExt.Value = LoginUserInfo.TelExt;
                SelectCity.ProvinceID = LoginUserInfo.ProvinceID;
                SelectCity.CityID = LoginUserInfo.CityID;
                if (LoginUserInfo.Sex==1)
                    rb1.Checked = true;
                else
                    rb0.Checked = true;
                imgimgPath.ImageUrl = LoginUserInfo.UserImage;
            }
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSaveClick(object sender, EventArgs e)
        {
            string strImg = "";
            var userModel = new UserBaseDac().GetModel(LoginUserInfo.ID);
            userModel.NickName = txtNickName.Value;
            userModel.RealName = txtRealName.Value;
            userModel.Birthday = Convert.ToDateTime(txtBirthday.Value);
            userModel.Sex = Convert.ToInt32(rb1.Checked ? "1" : "0");
            userModel.AreaCode = txtAreaCode.Value;
            userModel.Tel = txtTel.Value;
            userModel.TelExt = txtTelExt.Value;
            userModel.ProvinceID = Convert.ToInt32(SelectCity.ProvinceID);
            userModel.CityID = Convert.ToInt32(SelectCity.CityID);
            if (imgimgPath != null && imgimgPath.ImageUrl.IndexOf("Upload", StringComparison.Ordinal) > 0)
            {
                strImg = imgimgPath.ImageUrl.Substring(imgimgPath.ImageUrl.LastIndexOf('t') + 1);
                strImg = strImg.Substring(0, strImg.Length - 1);
            }
            else
            {
                if (imgimgPath != null) strImg = imgimgPath.ImageUrl.Replace("'", "");
            }
            userModel.UserImage = strImg;
            if (new UserBaseDac().Update(userModel) > 0)
            {
                Session[Constants.SESSION_WEB_USER_MODEL] = userModel;
                FunHelper.Alert("更新成功", "/Portal/UserCenter/UserInfo.aspx");
            }
            else
            {
                FunHelper.Alert("更新失败 ", "/Portal/UserCenter/UserInfo.aspx");
            }
        }

    }
}